package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.AttrDao;
import com.example.child_protection.dao.AttrValueDao;
import com.example.child_protection.dto.info.AttrDTO;
import com.example.child_protection.dto.info.AttrValueDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.AttrRequestDTO;
import com.example.child_protection.dto.request.find.CheckCodeRequestDTO;
import com.example.child_protection.dto.response.AttrListResponseDTO;
import com.example.child_protection.dto.response.AttrResponseDTO;
import com.example.child_protection.dto.response.CheckCodeResponseDTO;
import com.example.child_protection.enums.BooleanEnums;
import com.example.child_protection.enums.ResponseCodeEnums;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.AttrModel;
import com.example.child_protection.model.AttrValueModel;
import com.example.child_protection.model.SystemMenuButtonModel;
import com.example.child_protection.service.AttrQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 属性查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class AtrrQueryServiceImpl extends MyBaseService implements AttrQueryService {

    @Autowired
    public AttrDao attrDao;

    @Autowired
    public AttrValueDao attrValueDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public AttrResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.ATTR_FIND_ID, requestDTO, AttrResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, AttrResponseDTO>() {
            @Override
            protected AttrResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                AttrResponseDTO responseDTO = new AttrResponseDTO();
                AttrModel attrModel = attrDao.findId(requestDTO.getId());
                if(null == attrModel){
                    throw  new Exception(ResponseCodeEnums.NOT_MEET_CONDITIONS_DATA.getDisplayName());
                }
                AttrDTO attr = new AttrDTO();
                BeanUtils.copyProperties(attrModel, attr);

                AttrValueModel attrValueModel = new AttrValueModel();
                attrValueModel.setAttrCode(attr.getAttrCode());

                if(attr.getIsAttribute().equals(BooleanEnums.T)) {
                    attr.setAttrValue(JSONObject.parseArray(JSONArray.toJSONString(attrValueDao.findPage(attrValueModel)), AttrValueDTO.class));
                }
                responseDTO.setInfo(attr);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public AttrListResponseDTO findList(AttrRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.ATTR_FIND_LIST, requestDTO, AttrListResponseDTO.class, new MyServiceExecutor<AttrRequestDTO, AttrListResponseDTO>() {
            @Override
            protected AttrListResponseDTO myprocess(AttrRequestDTO requestDTO) throws Exception {
                AttrListResponseDTO responseDTO = new AttrListResponseDTO();
                AttrModel req = new AttrModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<AttrModel> lists = attrDao.findPage(req);
                List<AttrDTO> attrs = JSONObject.parseArray(JSONArray.toJSONString(lists), AttrDTO.class);
                AttrValueModel attrValueModel = new AttrValueModel();
                if(requestDTO.getIsOnlyRoot().equals(BooleanEnums.T)) {
                    for (AttrDTO attr : attrs) {
                        if (null != attr.getIsAttribute() && attr.getIsAttribute().equals(BooleanEnums.T)) {
                            attrValueModel = new AttrValueModel();
                            attrValueModel.setAttrCode(attr.getAttrCode());
                            attr.setAttrValue(JSONObject.parseArray(JSONArray.toJSONString(attrValueDao.findPage(attrValueModel)), AttrValueDTO.class));
                        }
                    }
                }

                responseDTO.setInfos(attrs);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询（根据类目ID查询）
     *
     * @param requestDTO
     * @return
     */
    public AttrListResponseDTO findListByCategoryId(AttrRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.ATTR_FIND_LIST_BY_CATEGORY_ID, requestDTO, AttrListResponseDTO.class, new MyServiceExecutor<AttrRequestDTO, AttrListResponseDTO>() {
            @Override
            protected AttrListResponseDTO myprocess(AttrRequestDTO requestDTO) throws Exception {
                AttrListResponseDTO responseDTO = new AttrListResponseDTO();
                AttrModel req = new AttrModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<AttrModel> lists = attrDao.findListByCategoryId(req);
                List<AttrDTO> attrs = JSONObject.parseArray(JSONArray.toJSONString(lists), AttrDTO.class);
                AttrValueModel attrValueModel = new AttrValueModel();
                if(requestDTO.getIsOnlyRoot().equals(BooleanEnums.T)) {
                    for (AttrDTO attr : attrs) {
                        if (null != attr.getIsAttribute() && attr.getIsAttribute().equals(BooleanEnums.T)) {
                            attrValueModel = new AttrValueModel();
                            attrValueModel.setAttrCode(attr.getAttrCode());
                            attr.setAttrValue(JSONObject.parseArray(JSONArray.toJSONString(attrValueDao.findPage(attrValueModel)), AttrValueDTO.class));
                        }
                    }
                }

                responseDTO.setInfos(attrs);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }



    /**
     * 检测CODE是否重复
     * @param requestDTO
     * @return
     */
    public CheckCodeResponseDTO checkCode(CheckCodeRequestDTO requestDTO){
        return this.service(ServiceActionEnums.ATTR_CHECK_CODE, requestDTO, CheckCodeResponseDTO.class, new MyServiceExecutor<CheckCodeRequestDTO, CheckCodeResponseDTO>() {
            @Override
            protected CheckCodeResponseDTO myprocess(CheckCodeRequestDTO requestDTO) throws Exception {
                CheckCodeResponseDTO res = new CheckCodeResponseDTO();
                AttrModel req = new AttrModel();
                req.setAttrCode(requestDTO.getCode());
                int result = attrDao.count(req);
                if (result > 0) {
                    res.setFlag(BooleanEnums.T);
                } else {
                    res.setFlag(BooleanEnums.F);
                }
                res.successfully();
                return res;
            }
        });
    }
}
